/**
 * @(#)ApkSimulatorTimer.java 2017年03月25日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import java.util.List;

import org.jsbd.boss.common.BaseThread;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.domian.UpgradeApkBean;
import org.jsbd.boss.service.IUpgradeApkService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 升级包版本定时器
 *  
 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
 * @version 1.0.0
 */
public class UpgradeApkTimer extends BaseThread {
	public static final Logger LOGGER = LoggerFactory.getLogger(UpgradeApkTimer.class);

	@Autowired
	private IUpgradeApkService upgradeApkService;
	@Autowired
	private RedisClient redisClient;

	@Override
	public void start() {
		try {
			if (redisClient.evalSetNx(CacheKey.UPGRADE_APK_LOCK_KEY, 6000)) {
				List<UpgradeApkBean> list = upgradeApkService.getAll();
				for (UpgradeApkBean uab : list) {
					Integer num = uab.getNum();
					if (num <= 0) {
						continue;
					}
					
					String mark = uab.getMark();
					Object obj = redisClient.evalHget(CacheKey.UPGRADE_APK_INDEX, mark);
					if (null == obj || Integer.valueOf(String.valueOf(obj)) <= 0) {
						redisClient.evalHset(CacheKey.UPGRADE_APK_INDEX, mark, String.valueOf(num));
					}
				}
			}
		} catch (Exception e) {
			LOGGER.error("更新", e);
		} finally {
			redisClient.remove(CacheKey.UPGRADE_APK_LOCK_KEY);
		}
	}
}
